﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// 
    /// </summary>
    public class ColorDifferenceUnsafe : BaseImageFilter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ColorDifferenceUnsafe"/> class.
        /// </summary>
        public ColorDifferenceUnsafe()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ColorDifferenceUnsafe"/> class.
        /// </summary>
        /// <param name="_colorDifferenceUnsafe">The _color difference unsafe.</param>
        internal ColorDifferenceUnsafe(ColorDifferenceUnsafe _colorDifferenceUnsafe)
        {
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            List<Color> aColor = new List<Color>();
            aColor.Add(bitmap.GetPixel(0, 0));
            int nCount = 1;

            Color colorPrev = Color.Transparent;
            Color colorCurrent = Color.Transparent;
            Color colorNew = Color.Transparent;

            int y = 0;
            int x = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    aColor.Add(bitmap.GetPixel(x, y));
                    colorPrev = aColor[nCount - 1];
                    colorCurrent = aColor[nCount];

                    colorNew = Color.FromArgb(
                        Math.Abs(colorPrev.R - colorCurrent.R), 
                        Math.Abs(colorPrev.G - colorCurrent.G), 
                        Math.Abs(colorPrev.B - colorCurrent.B));

                    bitmap.SetPixel(x, y, colorNew);
                    nCount++;
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new ColorDifferenceUnsafe(this);
        }
    }
}
